<template>
	<BpmComponent :processInstanceId="processInstanceId" @success="refreshDetail">
		<template #approval-info>
			<up-form labelPosition="left" labelWidth="80" v-if="Object.keys(formData).length > 0">
				<up-form-item v-for="field in formFields" :key="field.key" :label="field.label" borderBottom>
					<!-- 如果是附件 -->
					<view v-if="field.isDocument && formData[field.key]" class="file-actions">


						<template v-if="field.isArray">
							<template v-if="(formData[field.key] || []).length > 0">
								<view v-for="(item, idx) in formData[field.key]" :key="idx" class="file-item">
									<text class="file-link" @click="previewFile(item, formData[field.name]?.[idx])">
										{{ getFileName(formData[field.name]?.[idx]) || '无' }}
									</text>
									<!-- <wd-icon v-if="field.key === 'mainTextUrl'" name="upload" size="16px"
										color="#34d19d" style="margin-left:6px"
										@click="() => reUploadFile(field.key)" /> -->
								</view>
							</template>
							<template v-else>
								<text class="file-empty">无</text>
							</template>
						</template>

						<!-- 如果不是数组 -->
						<template v-else>
							<text class="file-link" @click="previewFile(formData[field.key], formData[field.name])">
								{{ getFileName(formData[field.name]) || '无' }}
							</text>
							<!-- <wd-icon name="delete-thin" size="16px" color="red" style="margin-left:8px"
							@click="deleteFile(field.key)" /> -->
							<wd-icon v-if="field.key === `mainTextUrl`" name="upload" size="16px" color="#409eff"
								style="margin-left:6px" @click="() => reUploadFile(field.key)" />
						</template>



					</view>

					<!-- 普通字段 -->
					<text v-else>
						{{ field.format ? field.format(formData[field.key]) : formData[field.key] || '无' }}
					</text>
				</up-form-item>
			</up-form>
		</template>
	</BpmComponent>
</template>

<script setup lang="ts">
import { onLoad } from "@dcloudio/uni-app";
import { ref } from 'vue'
import { DICT_TYPE, getDictLabel } from '@/utils/dict'
import BpmComponent from '@/components/processInstance/detail/index.vue'
import { ReceiveArticleHandlApi, ReceiveArticleHandleVO } from '@/api/oa/receivedManagement'
import { getFileName, parseArrayField, previewFile } from '@/utils/fileUtils'



const processInstanceId = ref(null)// 流程实例的编号
const detailId = ref(null)
const formData = ref<ReceiveArticleHandleVO>({} as any)
const formFields = [

	{ label: '编号', key: 'documentNumber' },
	{ label: '来文机关', key: 'fromAgency' },
	{ label: '来文文号', key: 'documentCode' },
	{
		label: '紧急程度', key: 'secretLevel', format: (val: any) => {
			return getDictLabel(DICT_TYPE.OA_SECRET_LEVEL, val)
		}
	},
	{ label: '事由', key: 'subject' },
	{ label: '收文时间', key: 'receptionTime' },
	{ label: '正文', key: 'mainTextUrl', isDocument: true, name: 'mainTextName', isArray: true },
	{ label: '附件', key: 'eids', isDocument: true, name: 'ename', isArray: true },

]


/** 获取页面详情 */
const getDetailById = async () => {
	try {
		const data = await ReceiveArticleHandlApi.getReceiveArticleHandle(Number(detailId.value))
		formData.value = data
		formData.value.eids = parseArrayField(data.eids);
		formData.value.ename = parseArrayField(data.ename);
		formData.value.mainTextUrl = parseArrayField(data.mainTextUrl);
		formData.value.mainTextName = parseArrayField(data.mainTextName);
	} catch (error) {
		console.log('加载数据失败')
	}
}
const refreshDetail = () => {
	console.log('父组件收到刷新事件，重新加载数据')
	getDetailById() // 调用父组件自己的获取详情方法
}
onLoad(async (options) => {
	processInstanceId.value = options?.processInstanceId
	detailId.value = options?.id
	/** 获得详情 */
	getDetailById()
})
</script>

<style>
/* 样式 */
/* 新增文件链接样式 */
.file-link {
	color: #007AFF;
	text-decoration: underline;
	cursor: pointer;
}

.uv-node {
	height: 34rpx;
	width: 34rpx;
	border-radius: 50%;
	background-color: #ddd;
	display: flex;
	align-items: center;
	justify-content: center;
	color: #fff;
	flex-shrink: 0;
	z-index: -1;
}

.audit-node {
	margin: 5px 0;
}
</style>